package com.project.smarthome.login.presenter;

import android.Manifest;

import com.project.smarthome.base.BasePresenter;
import com.project.smarthome.login.WelcomeContract;
import com.project.smarthome.login.view.WelcomeActivity;
import com.sds.smarthome.business.domain.UserService;
import com.sds.smarthome.business.domain.entity.GestureSet;
import com.sds.smarthome.business.domain.entity.UserInfo;
import com.sds.smarthome.business.domain.service.DomainFactory;
import com.sds.smarthome.business.domain.service.DomainService;
import com.tbruyelle.rxpermissions2.Permission;
import com.tbruyelle.rxpermissions2.RxPermissions;

import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;


/**
 * Created by L on 2016/5/18.
 */
public class WelcomePresenter extends BasePresenter implements WelcomeContract.Presenter {

    private WelcomeContract.View welcomeView;
    private final UserService userService;
    private final DomainService domainService;

    public WelcomePresenter(WelcomeContract.View welcomeView) {
        this.welcomeView = welcomeView;
        userService = DomainFactory.getUserService();
        domainService = DomainFactory.getDomainService();
    }

    @Override
    public void init() {
        UserInfo userInfo = null;
        if (userInfo == null) {
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e1) {
                e1.printStackTrace();
            }
            welcomeView.navToLogin();
            return;
        }
        Observable observable = Observable.create(new ObservableOnSubscribe<Boolean>() {
            @Override
            public void subscribe(ObservableEmitter<Boolean> e) throws Exception {
                boolean expired = userService.checkLocalTokenExpired();
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e1) {
                    e1.printStackTrace();
                }
                e.onNext(expired);
            }

        });
        Disposable subscribe = observable.
                subscribeOn(getJobExecutor()).
                observeOn(getUiExecutor()).
                subscribe(new Consumer<Boolean>() {
                    @Override
                    public void accept(Boolean expired) throws Exception {
                        if (!expired) {
                            GestureSet gestureSet = domainService.getGestureSet();
                            if (gestureSet != null && gestureSet.isOpen()) {
                                welcomeView.showGesture();
                            } else {
                                welcomeView.navToMain();
                            }
                        }
                    }

                });
        addSubscription(subscribe);
    }


    @Override
    public void checkPermission(WelcomeActivity welcomeActivity) {
        RxPermissions permissions = new RxPermissions(welcomeActivity);
        Disposable subscribe = permissions.requestEach(Manifest.permission.CAMERA,
                Manifest.permission.READ_EXTERNAL_STORAGE)
                .subscribe(new Consumer<Permission>() {
                    @Override
                    public void accept(Permission permission) throws Exception {

                    }
                });

        addSubscription(subscribe);
    }

    @Override
    public void destroy() {
        super.destroy();
        this.welcomeView = null;
    }
}
